package com.sharedlease.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

/**
 * @author Laymedown
 */
public final class AlipayUtil {

    /**
     *
     * @param out_trade_no : 订单号
     * @param subject : 订单主题
     * @param totalAmount : 订单金额
     * @param timeoutExpress : 超时时间 可为null
     * @param body : 订单描述（商品描述） 可 null
     * @param productCode : 商品码
     */
    public static String pay(String out_trade_no, String subject, String totalAmount, String timeoutExpress, String body,
                           String productCode,Integer isShareProduct) throws AlipayApiException {
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        String RETURN_URL="http://1.15.133.55:8090/pay/return";
        if (isShareProduct==1)
        {
            RETURN_URL="http://1.15.133.55:8090/pay/sharedReturn";
        }
        String NOTIFY_URL="http://1.15.133.55:8090/pay/notify";

        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(totalAmount);
        model.setTimeoutExpress(timeoutExpress);
        model.setBody(body);
        model.setProductCode(productCode);
        alipayRequest.setBizModel(model);

        try{
            return client.pageExecute(alipayRequest).getBody();
        }catch (AlipayApiException e){
            throw new AlipayApiException("支付宝支付失败", e);
        }
    }

    /**
     * 退款请求
     * @param out_trade_no : 商户订单号
     * @param trade_no : 支付宝交易号 注意 : trade_no、out_trade_no如果同时存在优先取trade_no
     * @param refund_amount : 退款金额
     * @param refund_reason : 退款原因
     * @param out_request_no : 标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传， 部分退款时的标记， 保证唯一性， 如果多次退款时， 需保证out_request_no不同
     * @return
     */
    public static AlipayTradeRefundResponse refund(String out_trade_no, String trade_no, String refund_amount, String refund_reason,
                                                   String out_request_no) throws AlipayApiException {
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model=new AlipayTradeRefundModel();
        model.setOutTradeNo(out_trade_no);
        model.setTradeNo(trade_no);
        model.setRefundAmount(refund_amount);
        model.setRefundReason(refund_reason);
        model.setOutRequestNo(out_request_no);
        alipayRequest.setBizModel(model);
        return client.execute(alipayRequest);
    }

    /**
     * 订单查询
     * @param out_trade_no : 商户订单号
     * @param trade_no : trade_no
     * @return
     * @throws AlipayApiException
     */
    public static AlipayTradeQueryResponse query(String out_trade_no, String trade_no) throws AlipayApiException {
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        AlipayTradeQueryModel model=new AlipayTradeQueryModel();
        model.setOutTradeNo(out_trade_no);
        model.setTradeNo(trade_no);
        alipayRequest.setBizModel(model);
        return client.execute(alipayRequest);
    }

    /**
     * 退款查询
     * @param out_trade_no
     * @param trade_no
     * @param out_request_no : 退款标记号
     * @return
     * @throws AlipayApiException
     */
    public static AlipayTradeFastpayRefundQueryResponse refundQuery(String out_trade_no, String trade_no, String out_request_no) throws AlipayApiException {
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();

        AlipayTradeFastpayRefundQueryModel model=new AlipayTradeFastpayRefundQueryModel();
        model.setOutTradeNo(out_trade_no);
        model.setTradeNo(trade_no);
        model.setOutRequestNo(out_request_no);
        alipayRequest.setBizModel(model);
        return client.execute(alipayRequest);
    }

}
